/**
 * @Author: easten
 * @Created: 2024-04-15 23:48:14
 * @Description:
 *
 */
import { ApiAttr } from "_common/api/iot";
import { DrawerProps } from "_common/DataType";
import { IotAttr } from "_common/entity";
import React, { useEffect, useRef } from "react";
import {
  Button,
  Drawer,
  Form,
  FormInstanceFunctions,
  Input,
  Select,
} from "tdesign-react";
import FormItem from "tdesign-react/es/form/FormItem";
type AttrDrawerProps = DrawerProps & {};
const AttrDrawer: React.FC<AttrDrawerProps> = (props: AttrDrawerProps) => {
  const { open, axios, edit, onSuccess, value } = props;
  const formRef = useRef<FormInstanceFunctions>();
  const apiAttr = new ApiAttr(axios!);
  useEffect(() => {
    return () => {
      formRef.current?.reset();
    };
  }, []);

  useEffect(() => {
    if (value && edit) {
      formRef.current?.setFieldsValue(value);
    }
  }, [edit, value]);

  const handleClose = () => {
    onSuccess?.(false);
    formRef.current?.reset();
  };

  // 新增属性
  const createAttr = async (data: IotAttr) => {
    const response = await apiAttr.createAttr(data);
    if (response && response.code == 200 && response.data) {
      handleClose();
    }
  };
  // 更新属性
  const updateAttr = async (data: IotAttr) => {
    const response = await apiAttr.updateAttr({
      ...data,
      id: value?.id,
    });
    if (response && response.code == 200 && response.data) {
      handleClose();
    }
  };

  return (
    <Drawer
      header={`${edit ? "编辑" : "添加"}属性`}
      footer={false}
      visible={open}
      sizeDraggable
      size="medium"
      onClose={handleClose}
    >
      <Form
        ref={formRef}
        labelWidth={100}
        onSubmit={async (data) => {
          // 是否校验通过
          if ((await formRef.current?.validateOnly()) == true) {
            edit
              ? updateAttr(data.fields as IotAttr)
              : createAttr(data.fields as IotAttr);
          }
        }}
      >
        <FormItem label="属性名称" rules={[{ required: true }]} name="attrName">
          <Input />
        </FormItem>
        <FormItem label="属性编码" rules={[{ required: true }]} name="attrCode">
          <Input />
        </FormItem>
        <FormItem label="单位" rules={[{ required: false }]} name="unit">
          <Input />
        </FormItem>
        <FormItem
          label="数据类型"
          rules={[{ required: false }]}
          name="dataType"
        >
          <Select
            options={[
              // int\bool\float\string
              { label: "integer", value: "integer" },
              { label: "boolean", value: "boolean" },
              { label: "float", value: "float" },
              { label: "string", value: "string" },
            ]}
          />
        </FormItem>
        <FormItem label="备注" rules={[{ required: false }]} name="remark">
          <Input />
        </FormItem>
        <FormItem className="float-right">
          <Button type="submit" theme="primary">
            提交
          </Button>
          <Button type="reset" theme="success" style={{ marginLeft: 12 }}>
            重置
          </Button>
        </FormItem>
      </Form>
    </Drawer>
  );
};
export default AttrDrawer;
